<?php

namespace app\api\controller\develop;
use app\api\service\develop\DatagridApiService;
use app\api\service\develop\DatagridColumnService;
use app\api\service\develop\DatagridToolbarService;
use app\api\service\develop\TableService;
use app\api\service\log\AdminOperateLogService;
use app\BaseController;
use app\api\service\develop\DatagridCustomService;
use app\util\AdminUserUtil;
use app\util\CommonUtil;
use app\util\ResponseUtil;
use app\util\UserUtil;

class DatagridCustom extends BaseController
{
    private $arrFieldTitle = array('datagrid_code' => '页面标识码', 'datagrid_name'=>'页面名称');

    public function getDatagridCustomInfo()
    {
        $id = $this->requestData['id'];
        $service = new DatagridCustomService();
        $finalData = $service->findRowById($id);
        return ResponseUtil::success($finalData);
    }

    public function getDatagridCustomList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new DatagridCustomService();
        $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
        $total = $service->getTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getDatagridCustomOption()
    {
        $service = new DatagridCustomService();
        $list = $service->getListNoPage($this->requestData);
        return ResponseUtil::success($list);
    }

    public function addDatagridCustom()
    {
        try{
            if(empty($this->requestData['datagrid_code'])){
                return ResponseUtil::failure("请填写数据表标识码");
            }
            if(empty($this->requestData['datagrid_name'])){
                return ResponseUtil::failure("请填写数据表名称");
            }
            $datagridCode = $this->requestData['datagrid_code'];
            $service = new DatagridCustomService();
            $datagridInfo = $service->findRowByData(['datagrid_code' => $datagridCode, 'status' => 1]);
            if($datagridInfo){
                return ResponseUtil::failure("相同唯一标识码的数据表已存在");
            }

            $serviceTable = new TableService();
            $infoTable = $serviceTable->findRowById($this->requestData['table_id']);
            if(!$infoTable){
                return ResponseUtil::failure("数据表不存在");
            }

            $this->requestData['table_name'] = $infoTable['table_name'];
            $res = $service->addDatagridCustom($this->requestData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($this->requestData, $this->arrFieldTitle);
                $message = '新增数据表';
                if($addInfo){
                    $message .= ','.$addInfo;
                }
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridCustom', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog('DatagridCustom', 'addDatagridCustom',  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增失败");
        }
    }

    public function editDatagridCustom()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的任务");
            }
            if(empty($this->requestData['datagrid_code'])){
                return ResponseUtil::failure("请填写数据表唯一标识码");
            }
            if(empty($this->requestData['datagrid_name'])){
                return ResponseUtil::failure("请填写数据表名称");
            }
            $id = $this->requestData['id'];
            $service = new DatagridCustomService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据表不存在");
            }
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData,['datagrid_code' => '页面标识码', 'datagrid_name'=>'页面名称', 'status'=>'状态']);
                $message = '编辑任务项,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => $oldInfo['datagrid_code'],
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridCustom', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog('DatagridCustom', 'editDatagridCustom',  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑失败");
        }
    }

    public function deleteDatagridCustom()
    {
        try{
            $id = $this->requestData['id'];
            $service = new DatagridCustomService();
            $info = $service->getDatagridCustomInfoById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->deleteDatagridCustomById($id);
            if($res){
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog('DatagridCustom', 'deleteDatagridCustom', CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除失败");
        }
    }

    public function getConfigureData()
    {
        try{
            if(empty($this->requestData['datagrid_code'])){
                return ResponseUtil::failure("参数datagrid_code不可为空");
            }
            $datagridCode = $this->requestData['datagrid_code'];
            $serviceDatagridCustom = new DatagridCustomService();
            $pageInfo = $serviceDatagridCustom->findRowByData(['datagrid_code' => $datagridCode, 'status'=>1]);
            if(!$pageInfo){
                return ResponseUtil::failure("页面不存在");
            }
            $serviceDatagridColumn = new DatagridColumnService();
            $serviceDatagridToolbar = new DatagridToolbarService();
            $finalData = array();
            $condition = ['datagrid_id' => $pageInfo['id'], 'status' => 1];
            $finalData['datagrid_info'] = $pageInfo;
            $notFixCondition = array_merge($condition, array('is_fix' => 2));
            $finalData['datagrid_column_list'] = $serviceDatagridColumn->getListNoPage($notFixCondition);
            $fixCondition = array_merge($condition, array('is_fix' => 1));
            $finalData['datagrid_column_list_fix'] = $serviceDatagridColumn->getListNoPage($fixCondition);
            $finalData['datagrid_request_list'] = $serviceDatagridToolbar->getPageAllConfigureApi($condition);
            $finalData['datagrid_refresh_path'] = $serviceDatagridToolbar->getDatagridRefreshApi($condition);
            $finalData['datagrid_toolbar_list'] = $serviceDatagridToolbar->getListNoPage($condition);
            return ResponseUtil::success($finalData);
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __METHOD__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("获取失败");
        }
    }
}



